<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of User
 *
 * @author phuctq
 */
class User {
    //put your code here
    private $id;
    private $userName;
    private $password;
    private $status;
    private $email;
    private $address;
    private $phone;
    private $fullName;
    /*
    public function __construct() {
        ;
    } */   
    public function __construct($username = "",$password ="",$email="",$phone="",
            $address="",$status =1,$id = 0,$fullName = ""){
        $this->userName = $username;
        $this->password = $password;
        $this->email = $email;
        $this->phone = $phone;        
        $this->address = $address;
        $this->status = $status;
        $this->id = $id;
        $this->fullName = $fullName;
    }
    
    public function SetUserName($userName){
        $this->userName = $userName;
    }
    
    public function SetPassWord($password){
        $this->password = $password;
    }
    
    public function GetUserName(){
        return $this->userName;
    }
    public function GetPassWord(){
       return $this->password ;
    }
    public function GetId(){
       return $this->id ;
    }
    //tap json chu doi tuong user
    public function getJson(){
        $item = array(
            'id' => $this->id,
            'username' => $this->userName,
            'password' => $this->password,
            'email' => $this->email,
            'address' =>  $this->address,
            'phone' =>  $this->phone,
            'status' =>  $this->status,
            'fullname' => $this->fullName
        );
        return $item;
    }
}

class UserModel{
    public function Insert($user){
        //biet su dung dung tham so trong php thi rat tot
        $procName = "CALL sp_users(?,?,?,?)";//ten Proc(Mỗi dấu hỏi tương ứng với 1 tham số
        $names = array("p_id","p_userName","p_password","p_t");//Mảng Chứa tên các tham số của Proc.
        $types = array(Statement_Parameter_Type ::$STATEMENT_TYPE_INTEGER,Statement_Parameter_Type::$STATEMENT_TYPE_STRING,
            Statement_Parameter_Type ::$STATEMENT_TYPE_STRING,Statement_Parameter_Type::$STATEMENT_TYPE_INTEGER);
        //mảng chứa các kiểu dữ liệu tương ứng với giá trị
        $values = array($user->GetId(),$user->GetUserName(),$user->GetPassWord(),0);// 0 insert
        DataProvider::ExecuteQueryParamter($procName, $names, $values, $types);
    }
    
    public function Edit($user){
        $procName = "CALL sp_users(?,?,?,?)";
        $names = array("p_id","p_userName","p_password","p_t");
        $types = array(Statement_Parameter_Type ::$STATEMENT_TYPE_INTEGER,Statement_Parameter_Type::$STATEMENT_TYPE_STRING,
            Statement_Parameter_Type ::$STATEMENT_TYPE_STRING,Statement_Parameter_Type::$STATEMENT_TYPE_INTEGER);
        $values = array($user->GetId(),$user->GetUserName(),$user->GetPassWord(),1);//1 edit
        DataProvider::ExecuteQueryParamter($procName, $names, $values, $types);
    }
    
    public function Delete($id){
         $procName = "CALL sp_users(?,?,?,?)";
        $names = array("p_id","p_userName","p_password","p_t");
        $types = array(Statement_Parameter_Type ::$STATEMENT_TYPE_INTEGER,Statement_Parameter_Type::$STATEMENT_TYPE_STRING,
            Statement_Parameter_Type ::$STATEMENT_TYPE_STRING,Statement_Parameter_Type::$STATEMENT_TYPE_INTEGER);
        $values = array($user->GetId(),$user->GetUserName(),$user->GetPassWord(),2);//2 delete
        DataProvider::ExecuteQueryParamter($procName, $names, $values, $types);
    }

    // Hien thi thong tin 1 user
    public function GetUserById($id){
        $mysql = "select * from Users where id =" + $id + ";";        
        $results = DataProvider::ExecuteQuery($mysql);
        $data = array();
        if($results != FALSE){
            while ($row = mysql_fetch_array($results,MYSQL_ASSOC)){
                $item = new User($row["userName"],$row["password"],$row["email"],
                        $row["phone"],$row["address"],(int)$row["status"],(int)$row["id"],$row["fullName"]);
                $data[] = $item->getJson();
            }
        }    
        return $data;
    }
    
    //lay tat ca danh sach users
    public function GetAllUser(){
        $mysql = "select * from Users;";
        $results = DataProvider::ExecuteQuery($mysql);
        $data = array();
        if($results != FALSE){
            while ($row = mysql_fetch_array($results,MYSQL_ASSOC)){
                $item = new User($row["userName"],$row["password"],$row["email"],
                        $row["phone"],$row["address"],(int)$row["status"],(int)$row["id"],$row["fullName"]);
                $data[] = $item->getJson();
            }
        }
        return $data;//tra ve mot mang json
    }
}
?>
